Imports System.Net
Imports System.IO
Imports System.Text

Public MustInherit Class BaseDownloader

    Public Event DownloadingItem(ByVal itemId As Integer)

    Protected _imageDirectory As String
    Protected _imageHolder As PictureBox

    Protected _linkFile As New LinkFile(LinkFilename)

    Protected Const EMPTY As String = ""

    Protected Const JPEG_EXTENSION As String = ".jpg"

    Protected Const FINISHED_MESSAGE As String = "Finished"

    Protected PageNotFoundMessage As String = "The remote server returned an error: (404) Not Found."

    Public MustOverride ReadOnly Property BasePageUrl() As String
    Public MustOverride ReadOnly Property BaseImageUrl() As String
    Public MustOverride ReadOnly Property LinkFilename() As String
    Public MustOverride ReadOnly Property UrlToRemove() As String

    Public Sub New(ByVal imageDirecory As String, _
                   ByVal imageHolder As PictureBox)

        _imageDirectory = imageDirecory
        _imageHolder = imageHolder

    End Sub

    Public Overridable Function DownloadImage(ByVal productCode As String, _
                                              ByVal productUrl As String) As String

        Dim productId As Integer = 0

        Try
            productId = CType(productUrl.Replace(UrlToRemove, EMPTY), Integer)
        Catch ex As Exception
            Throw New InvalidProductUrlException
        End Try

        DownloadImage(productId, _
                      productCode)

        Return productCode & JPEG_EXTENSION

    End Function


    Protected Overridable Sub DownLoadImage(ByVal imageId As Integer, _
                                            ByVal productCode As String)


        Try
            Dim fileName As String = My.Settings.ImageDirectory
            fileName &= productCode
            fileName &= JPEG_EXTENSION

            _imageHolder.Load(String.Format(BaseImageUrl, imageId))
            _imageHolder.Image.Save(fileName, _
                                    System.Drawing.Imaging.ImageFormat.Jpeg)
            Application.DoEvents()
        Catch ex As System.Net.WebException
            If ex.Status <> WebExceptionStatus.ProtocolError Then
                MsgBox(ex.Message)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

        End Try

    End Sub

    Protected Function GetPageContents(ByVal itemId As Integer) As String

        Dim request As HttpWebRequest = CType(WebRequest.Create(String.Format(BasePageUrl, itemId.ToString)), _
                                                  HttpWebRequest)
        request.KeepAlive = False

        Dim response As HttpWebResponse = CType(request.GetResponse(), _
                                                HttpWebResponse)

        Dim receiveStream As Stream = response.GetResponseStream()
        Dim readStream As New StreamReader(receiveStream, _
                                           Encoding.UTF8)

        Dim pageContents As String = readStream.ReadToEnd

        readStream.Close()
        response.Close()

        Return pageContents

    End Function

    Protected MustOverride Sub DownLoadPage(ByVal itemId As Integer)

    Protected Sub Download(ByVal maxId As Integer)

        For itemId As Integer = 1 To maxId

            RaiseEvent DownloadingItem(itemId)
            DownLoadPage(itemId)

        Next

        MsgBox(FINISHED_MESSAGE)

    End Sub

    Protected Sub Download(ByVal minId As Integer, _
                           ByVal maxId As Integer)

        If minId <= maxId Then

            For itemId As Integer = minId To maxId

                RaiseEvent DownloadingItem(itemId)
                DownLoadPage(itemId)

            Next

        End If

        MsgBox(FINISHED_MESSAGE)

    End Sub

    Public MustOverride Sub Download()

End Class
